import os
import sys
import config_setting
from logger import Logger
from datetime import datetime
from check_symbols import check_symbols
from sync_symbols import sync_newly_symbols, sync_removal_symbols

if __name__ == "__main__":
    if not os.path.isdir(f"assets"):
        os.mkdir(f"assets")
    if not os.path.isdir(f"assets/{config_setting.batch_name}"):
        os.mkdir(f"assets/{config_setting.batch_name}")
    if not os.path.isdir(f"assets/{config_setting.batch_name}/log"):
        os.mkdir(f"assets/{config_setting.batch_name}/log")
    sys.stdout = Logger(datetime.now().strftime(f"assets/{config_setting.batch_name}/log/%Y%m%d_%H%M%S") + ".txt")

    sync_group_list = [
        {
            "sync_name": "sync",
            "left_side_path": r"\\diskstation2\8192G_01",
            "right_side_path": r"\\diskstation2\8192G_02"
        },
        {
            "sync_name": "sync2",
            "left_side_path": rf"\\diskstation2\4096G_RAID",
            "right_side_path": rf"\\diskstation2\8192G_01"
        },
        {
            "sync_name": "sync3",
            "left_side_path": r"\\diskstation2\8192G_01",
            "right_side_path": r"\\diskstation2\8192G_02"
        },
    ]

    # sync_name = "sync2"

    # left_side_path = r"\\diskstation2\8192G_01"
    # right_side_path = r"\\diskstation2\8192G_02"

    for sync_group in sync_group_list:
        left_side_path = sync_group["left_side_path"]
        right_side_path = sync_group["right_side_path"]
        sync_suffix = f"__{sync_group['sync_name']}"
        symbol_folder = f"{sync_group['sync_name']}_goodsync"
        print(f"------------------------------------------------------------------------------\nSTARTing GROUP {sync_group['sync_name']}\n")
        # 左边 -> e.g. 8192G_01
        print(f"=================执行左边 {left_side_path}...")
        newly_sync_list, invalid_symbol_list, removal_sync_list = check_symbols(left_side_path, sync_suffix, symbol_folder)
        if len(newly_sync_list) == 0 and \
            len(removal_sync_list) == 0 and \
            len(invalid_symbol_list) == 0:
            print(f"\033[32m[Success]\033[0m 无问题，不用执行脚本")
        if len(newly_sync_list) > 0:
            print(f"\033[32m[Next]\033[0m 有新增待同步文件，需执行脚本")
            sync_newly_symbols(left_side_path, right_side_path, newly_sync_list, sync_suffix, symbol_folder)
        if len(removal_sync_list) > 0:
            print(f"\033[31m[Errors]\033[0m 有去除的同步文件，需执行脚本（不应该出现）")  # 这个一般检查不出，因为在windows上识别不出已经无效的符号链接。
            sync_removal_symbols(left_side_path, right_side_path, removal_sync_list, sync_suffix, symbol_folder)
        if len(invalid_symbol_list) > 0:
            print(f"\033[32m[Next]\033[0m 有去除的同步文件（应该是手动改名导致），请执行同步脚本")
            sync_removal_symbols(left_side_path, right_side_path, invalid_symbol_list, sync_suffix, symbol_folder)


        # 右边 -> e.g. 8192G_02
        print(f"=================执行右边 {right_side_path}...")
        newly_sync_list, invalid_symbol_list, removal_sync_list = check_symbols(right_side_path, sync_suffix, symbol_folder)
        if len(newly_sync_list) == 0 and \
            len(removal_sync_list) == 0 and \
            len(invalid_symbol_list) == 0:
            print(f"\033[32m[Success]\033[0m 无问题，不用执行脚本")
        if len(newly_sync_list) > 0:
            print(f"\033[32m[Next]\033[0m 有新增待同步文件，需执行脚本")
            sync_newly_symbols(right_side_path, left_side_path, newly_sync_list, sync_suffix, symbol_folder)
        if len(removal_sync_list) > 0:
            print(f"\033[31m[Errors]\033[0m 有去除的同步文件，需执行脚本（不应该出现）")  # 这个一般检查不出，因为在windows上识别不出已经无效的符号链接。
            sync_removal_symbols(right_side_path, left_side_path, removal_sync_list, sync_suffix, symbol_folder)
        if len(invalid_symbol_list) > 0:
            print(f"\033[32m[Next]\033[0m 有去除的同步文件（应该是手动改名导致），请执行同步脚本")
            sync_removal_symbols(right_side_path, left_side_path, invalid_symbol_list, sync_suffix, symbol_folder)